package cn.waka.sys.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.service.BoxService;
import cn.waka.sys.admin.service.DeviceService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private BoxService boxService;

    @RequestMapping(path = "/box", name = "机顶盒列表")
    public ModelAndView box(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }
        String device_num = req.getParameter("device_num");
        ModelAndView mv = new ModelAndView();
        Map<String, String> param = new HashMap<String, String>();
        String search_param = "";
        if (device_num != null && device_num.length() > 0) {
            param.put("device_num", "%" + device_num + "%");
            search_param += "&device_num=" + device_num;
            mv.addObject("search_device_num", device_num);
        }

        deviceService.findBoxPage(p, param);

        mv.addObject("page", p);
        mv.addObject("search_param", search_param);
        return mv;

    }

    @RequestMapping(path = "/log", name = "机顶盒日志")
    public ModelAndView pageLogList(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        String idStr = req.getParameter("id");
        if (idStr == null || !Validator.isNum(idStr)) {
            idStr = "0";
        }
        int box_id = Integer.parseInt(idStr);

        ModelAndView mv = new ModelAndView();

        boxService.getBoxLogList(box_id, p);
        p.addParam("box_id", idStr);
        mv.addObject("page", p);
        mv.addObject("box_id", idStr);
        return mv;

    }

}
